﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WTSQCB
{
    public partial class DiemTheoLopHoc : System.Web.UI.Page
    {
        private string chuoiketnoi = ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                CleartextBoxes(this);
                MaHocVienrqf.Enabled = false;
                TenHocVienrqf.Enabled = false;
                DiemQTrqf.Enabled = false;
                DiemQTrv.Enabled = false;
                DiemThirf.Enabled = false;
                DiemThirv.Enabled = false;
            }
            else
            {
                MaHocVienrqf.Enabled = true;
                TenHocVienrqf.Enabled = true;
                DiemQTrqf.Enabled = true;
                DiemQTrv.Enabled = true;
                DiemThirf.Enabled = true;
                DiemThirv.Enabled = true;
            }            
        }
        public void CleartextBoxes(Control parent)
        {

            foreach (Control c in parent.Controls)
            {
                if ((c.GetType() == typeof(TextBox)))
                {

                    ((TextBox)(c)).Text = "";
                }

                if (c.HasControls())
                {
                    CleartextBoxes(c);
                }
            }
        }
        KetNoiDuLieu kn = new KetNoiDuLieu();
        protected void Thembtn_Click(object sender, EventArgs e)
        {                       
                try
                {
                    //cập nhật từ dòng từ bảng tạm vào CSDL ở bang DiemThiMonHoc
                    //foreach (GridViewRow dong in this.DiemThidgv.Rows)
                    
                    
                    for (int i = 1; i <= DiemThidgv.Rows.Count; i++ )
                    {
                        SqlConnection con = new SqlConnection(chuoiketnoi);
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = con;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "DiemThiThem";
                        cmd.Parameters.AddWithValue("@mahocvien", DiemThidgv.Rows[i-1].Cells[1].Text);
                        cmd.Parameters.AddWithValue("@mamonhoc", MaMonHoctxt.Text);
                        cmd.Parameters.AddWithValue("@malop", MaLoptxt.Text);
                        cmd.Parameters.AddWithValue("@mahocki", MaHKtxt.Text);
                        cmd.Parameters.AddWithValue("@diemquatrinh", DiemThidgv.Rows[i-1].Cells[6].Text);
                        cmd.Parameters.AddWithValue("@diemthi", DiemThidgv.Rows[i-1].Cells[7].Text);                        
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        //DiemThidgv.DataBind();                        
                    }
                    Response.Write("Cập nhật thành công");
                }
                catch (System.Exception ex)
                {
                    Response.Write("không thực hiện được");
                }
            
            
        }

        protected void Suabtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;                
                cmd.CommandText = "BangNhapDiemSua";
                cmd.Parameters.AddWithValue("@mahocvien", MaHVtxt.Text);               
                cmd.Parameters.AddWithValue("@diemquatrinh", DiemQTtxt.Text);
                cmd.Parameters.AddWithValue("@diemthi", DiemThitxt.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                DiemThidgv.DataBind();                
                Response.Write("Nhập điểm thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }
                
        protected void DiemThidgv_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }
        
        protected void Locbtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "ChenHocVienTheoLopMonHocHocKi";                
                cmd.Parameters.AddWithValue("@malop", MaLoptxt.Text);
                cmd.Parameters.AddWithValue("@mamonhoc", MaMonHoctxt.Text);
                cmd.Parameters.AddWithValue("@mahocki", MaHKtxt.Text);                
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                DiemThidgv.DataBind();
                Response.Write("Load thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }
        public void LoadGirdView()
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                con.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "BangNhapDiemLoad";                                
                //cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DiemThidgv.DataSource = ds;
                DiemThidgv.DataBind();
                Response.Write("Load thành công");
                con.Close();
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void DiemThidgv_SelectedIndexChanged1(object sender, EventArgs e)
        {
            GridViewRow dong = DiemThidgv.SelectedRow;
            MaHVtxt.Text = HttpUtility.HtmlDecode(dong.Cells[1].Text);
            TenHVtxt.Text = HttpUtility.HtmlDecode(dong.Cells[2].Text);
            DiemQTtxt.Text = HttpUtility.HtmlDecode(dong.Cells[6].Text);
            DiemThitxt.Text = HttpUtility.HtmlDecode(dong.Cells[7].Text); 
        }     
    }
}